package com.yxh.util;

import com.alibaba.fastjson.JSONArray;
import com.yxh.pojo.Menu;
import com.yxh.pojo.tree.BaseTreeGrid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by 32988 on 2018/5/15.
 */
public class TreeUtil {

    public List<BaseTreeGrid> transListTree(Collection<Menu> listMenu) {
        List<BaseTreeGrid> list = new ArrayList<BaseTreeGrid>();
        for ( Menu menu: listMenu) {
            BaseTreeGrid root = new BaseTreeGrid();
            root.setTitle(menu.getMenuName());
            root.setId(menu.getMenuId());
            root.setParentId(menu.getParentId() == null ? "" : menu.getParentId());
            root.setName(menu.getMenuUrl());
            root.setRoles(0);
            list.add(root);
        }
        List<BaseTreeGrid> treelist = formatTree(list, false);
        String json = JSONArray.toJSONString(treelist);
        System.out.println(json);
        return treelist;
    }

    public List<BaseTreeGrid> transListTree(List<BaseTreeGrid> listMenu) {
        List<BaseTreeGrid> list = new ArrayList<BaseTreeGrid>();
        List<BaseTreeGrid> treelist = formatUserTree(listMenu, true);
        String json = JSONArray.toJSONString(treelist);
        System.out.println(json);
        return treelist;
    }

    public List<BaseTreeGrid> transTypeTree(List<BaseTreeGrid> listMenu) {
        List<BaseTreeGrid> list = new ArrayList<BaseTreeGrid>();
        List<BaseTreeGrid> treelist = formatTree(listMenu, true);
        String json = JSONArray.toJSONString(treelist);
        System.out.println(json);
        return treelist;
    }

    public static <T extends BaseTreeGrid> List<T> formatTree(List<T> list, Boolean flag) {
        List<T> nodeList = new ArrayList<T>();
        for (T node1 : list) {
            boolean mark = false;
            for (T node2 : list) {

                if (node1.getParentId() != null && node1.getParentId().equals(node2.getId())) {
                    mark = true;
                    if (node2.getChildren() == null) {
                        node2.setChildren(new ArrayList<BaseTreeGrid>());
                    }
                    node2.getChildren().add(node1);
                    break;
                }
            }
            if (!mark) {
                nodeList.add(node1);
            }
        }
        return nodeList;
    }

    public static <T extends BaseTreeGrid> List<T> formatUserTree(List<T> list, Boolean flag) {
        List<T> nodeList = new ArrayList<T>();
        for (T node1 : list) {
            boolean mark = false;
            for (T node2 : list) {

                if (node1.getParentId() != null && node1.getParentId().equals(node2.getId())) {
                    mark = true;
                    if (node2.getChildren() == null) {
                        node2.setChildren(new ArrayList<BaseTreeGrid>());
                    }
                    node2.getChildren().add(node1);
                    break;
                }
                //部门继续增加空子节点  区别人员和部门
                if(node2.getIsDept().equals("1")){
                    int i=0;
                    for(T node3 : list){
                        if(!node1.getParentId().equals(node3.getId())){
                            i++;
                        }
                        else break;
                    }
                    if(i==list.size())
                        node2.setChildren(new ArrayList<BaseTreeGrid>());
                }
            }
            if (!mark) {
                nodeList.add(node1);
            }
        }
        return nodeList;
    }
}

